<template>
  <j-modal
    :title="$t('结账单')"
    :width="width"
    :visible="visible"
    switchFullscreen
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
  >
    <template slot="footer">
      <a-button type="primary" v-print="'#printContent'">{{ $t('打印') }}</a-button>
      <a-button @click="handleCancel">{{ $t('关闭') }}</a-button>
    </template>
    <div id="printContent">
      <div style="display: flex; padding-bottom: 4px; padding-left: 20px; padding-top: 20px">
        <div style="width: 34%">{{ $t('结账单号') }}：{{ model.id }}</div>
        <div style="width: 44%">{{$t('账目开始时间')}}：{{ model.startTime }}</div>
      </div>
      <div style="display: flex; padding-bottom: 4px; padding-left: 20px">
        <div style="width: 34%">{{ $t('设备') }}：{{ model.deviceName }}（{{ model.deviceId }}）</div>
        <div style="width: 34%">{{ $t('操作员') }}：{{ model.operator }}</div>
      </div>
      <div style="display: flex; padding-bottom: 4px; padding-left: 20px">
        <div style="width: 34%">{{ $t('结账人') }}：{{ model.createBy }}</div>
        <div style="width: 34%">{{ $t('结账时间') }}：{{ model.createTime }}</div>
      </div>
      <ul style="margin-bottom:4px" v-if="dataSource4.length>0">
        <li style="font-weight: bold">{{ $t('收款明细') }}</li>
      </ul>
      <div style="padding-left: 20px; padding-bottom: 4px" v-if="dataSource4.length>0">
        <a-table size="middle" :columns="columns4" :dataSource="dataSource4" :pagination="false" bordered></a-table>
      </div>
      <ul style="margin-bottom:4px" v-if="dataSource5.length>0">
        <li style="font-weight: bold">{{ $t('商品销售明细') }}</li>
      </ul>
      <div style="padding-left: 20px; padding-bottom: 4px" v-if="dataSource5.length>0">
        <a-table size="middle" :columns="columns5" :dataSource="dataSource5" :pagination="false" bordered></a-table>
      </div>
      <ul style="margin-bottom:4px" v-if="dataSource6.length>0">
        <li style="font-weight: bold">{{ $t('业务明细') }}</li>
      </ul>
      <div style="padding-left: 20px; padding-bottom: 4px" v-if="dataSource6.length>0">
        <a-table size="middle" :columns="columns6" :dataSource="dataSource6" :pagination="false" bordered></a-table>
      </div>
    </div>
  </j-modal>
</template>

<script>
import { getAction } from '@/api/manage'
export default {
  name: 'CheckOutPrintModal',
  components: {},
  data() {
    return {
      width: 800,
      visible: false,
      disableSubmit: false,
      model: {},
      columns5: [
        {
          title: this.$t('商品形态'),
          align: 'center',
          dataIndex: 'form_dictText',
          customRender: (t, r) => {
            return {
              children: t,
              attrs: {
                rowSpan: r.form_dictTextRowSpan,
              },
            }
          },
        },
        {
          title: this.$t('商品名称'),
          align: 'center',
          dataIndex: 'goodsName',
          customCell: (t,r) => {
            return {
              style :{
                maxWidth: '280px',
                whiteSpace: 'pre-wrap',
                wordBreak:'break-all'
              }
            }
          }
        },
        {
          title: this.$t('数量'),
          align: 'center',
          dataIndex: 'saleAmount',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('金额'),
          align: 'center',
          dataIndex: 'saleTotle',
          customRender: function (text) {
            return text.toFixed(2)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('兑换数量'),
          align: 'center',
          dataIndex: 'exchangeAmount',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
      ],
      columns6: [
        {
          title: this.$t('业务类型'),
          align: 'center',
          dataIndex: 'typeId_dictText',
        },
        {
          title: this.$t('数量'),
          align: 'center',
          dataIndex: 'count',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('金额'),
          align: 'center',
          dataIndex: 'payAmount',
          customRender: function (text) {
            return text.toFixed(2)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
      ],
      columns4: [
        {
          title: this.$t('收款方式'),
          align: 'center',
          dataIndex: 'payType_dictText',
        },
        {
          title: this.$t('收款笔数'),
          align: 'center',
          dataIndex: 'count',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('应收金额'),
          align: 'center',
          dataIndex: 'shouldPay',
          customRender: function (text) {
            return text.toFixed(2)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('实收'),
          align: 'center',
          dataIndex: 'actualPay',
          customRender: function (text) {
            return text.toFixed(2)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('差额'),
          align: 'center',
          dataIndex: 'diff',
          customRender: function (text) {
            return text.toFixed(2)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
      ],
      dataSource4: [],
      dataSource5: [],
      dataSource6: [],
      taxSetting: localStorage.getItem('taxSetting')
    }
  },
  methods: {
    rowSpan(key) {
      let _list = this.dataSource5
      let _num = [] // 相同的数据先存储在一起
      let indexList = [] // 相同数据的下标数组

      for (let i = 0; i < _list.length; i++) {
        //  最后一条的 +1 时会为undefind 报错，所以需要给判断
        let downKey = _list[i + 1] ? _list[i + 1][key] : ''
        // 当与下一条数据不同时，添加当前数据到_num,并进行处理
        if (_list[i][key] != downKey) {
          _num.push(_list[i])
          indexList.push(i) //获取相同的 下标

          // 遍历相同数据的数组
          for (let z = 0; z < _num.length; z++) {
            // 第一个设置需要合并的数值
            _list[indexList[0]][`${key}RowSpan`] = _num.length
            if (z != 0) {
              //其他设置为0
              _list[indexList[z]][`${key}RowSpan`] = 0
            }
          }
          //  当与下一行不同时，切记清除数组，因为一列会有多个合并表格
          _num = []
          indexList = []
          continue
        } else {
          //当与下一条数据不同时，添加当前数据到_num,不进行处理
          _num.push(_list[i])
          indexList.push(i)
        }
      }
      // 重新渲染数据
      this.dataSource5 = _list
    },
    show(record, state) {
      let that = this
      that.taxSetting = localStorage.getItem('taxSetting')
      that.columns4 = [
        {
          title: this.$t('收款方式'),
          align: 'center',
          dataIndex: 'payType_dictText',
        },
        {
          title: this.$t('收款笔数'),
          align: 'center',
          dataIndex: 'count',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('应收金额'),
          align: 'center',
          dataIndex: 'shouldPay',
          customRender: function (text) {
            return text.toFixed(2)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('实收'),
          align: 'center',
          dataIndex: 'actualPay',
          customRender: function (text) {
            return text.toFixed(2)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('差额'),
          align: 'center',
          dataIndex: 'diff',
          customRender: function (text) {
            return text.toFixed(2)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
      ]
      if (that.taxSetting != 0) {
        that.columns4.push({
          title: this.$t('其中税费'),
          align: 'center',
          dataIndex: 'taxPrice',
          customRender: function (text) {
            return text.toFixed(2)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        })
      }
      that.columns6 = [
        {
          title: this.$t('业务类型'),
          align: 'center',
          dataIndex: 'typeId_dictText',
        },
        {
          title: this.$t('数量'),
          align: 'center',
          dataIndex: 'count',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('金额'),
          align: 'center',
          dataIndex: 'payAmount',
          customRender: function (text) {
            return text.toFixed(2)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        }
      ]
      if (localStorage.getItem('operateCoin') == 1) {
        this.columns6.splice(this.columns6.length, 0, {
          title: this.$t('代币'),
          align: 'center',
          dataIndex: 'coin',
          customRender: function (text) {
             return text.toFixed(2)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        })
      }
      if (localStorage.getItem('operateLottery') == 1) {
        this.columns6.splice(this.columns6.length, 0, {
          title: this.$t('彩票'),
          align: 'center',
          dataIndex: 'lottery',
          customRender: function (text) {
             return text.toFixed(0)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        })
      }
      if (localStorage.getItem('operateCredit') == 1) {
        this.columns6.splice(this.columns6.length, 0, {
          title: this.$t('积分'),
          align: 'center',
          dataIndex: 'integral',
          customRender: function (text) {
             return text.toFixed(0)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        })
      }
      if (localStorage.getItem('operateALottery') == 1) {
        this.columns6.splice(this.columns6.length, 0, {
          title: this.$t('A票'),
          align: 'center',
          dataIndex: 'alottery',
          customRender: function (text) {
             return text.toFixed(0)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        })
      }
      if (localStorage.getItem('operateACoin') == 1) {
        this.columns6.splice(this.columns6.length, 0, {
          title: this.$t('A币'),
          align: 'center',
          dataIndex: 'points',
          customRender: function (text) {
             return text.toFixed(0)
          },
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        })
      }
      that.dataSource4 = []
      that.dataSource5 = []
      that.dataSource6 = []
      that.visible = true
      that.model = Object.assign({}, record)
      getAction('/checkout/bisCheckOut/queryBisCheckOutGoodsByMainId', {
        id: that.model.id,
        groupBy: 'goods_id,goods_name,form',
        orderType: '2',
        dataType: that.model.dataType,
      }).then((res) => {
        if (res.success) {
          that.dataSource5 = res.result
          if (that.dataSource5.length > 0) {
            that.addSumRow(['goodsName', 'saleAmount', 'saleTotle', 'exchangeAmount'], that.dataSource5)
            that.rowSpan('form_dictText')
          }
        }
      })
      getAction('/checkout/bisCheckOut/queryBisCheckOutTypeByMainId', {
        id: that.model.id,
        dataType: that.model.dataType,
      }).then((res) => {
        if (res.success) {
          that.dataSource6 = res.result
          if (that.dataSource6.length > 0) {
            that.addSumRow(
              ['typeId_dictText', 'count', 'payAmount', 'coin', 'lottery', 'integral', 'points', 'alottery'],
              that.dataSource6
            )
          }
        }
      })
      getAction('/checkout/bisCheckOut/queryBisCheckOutPayByMainId', {
        id: that.model.id,
        dataType: that.model.dataType,
      }).then((res) => {
        if (res.success) {
          that.dataSource4 = res.result
          if (that.dataSource4.length > 0) {
            that.addSumRow(['payType_dictText', 'count', 'shouldPay', 'actualPay', 'diff', 'taxPrice'], that.dataSource4)
          }
        }
      })
    },
    addSumRow(myArr, dataSource) {
      let total = {}
      myArr.forEach((tcn, ind) => {
        if (ind == 0) {
          total[tcn] =  this.$t('合计')
        } else {
          total[tcn] = 0
        }
      })
      for (let key in total) {
        dataSource.forEach(function (ds) {
          if (!isNaN(total[key])) {
            total[key] += Number(ds[key])
          }
        })
      }
      dataSource.push(total)
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>